package library;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import library.model.DownloadFilePojo;

/**
 * @author Dmitry
 */
public class DownloadFilesTableModel extends AbstractTableModel {
    private String[] COLUMNS = {"Файл", "Статус", ""};
    private final int FILE = 0;
    private final int STATUS = 1;
    private final int DELETE = 2;
    private List<DownloadFilePojo> data = new ArrayList<>();

    public void setData(List<DownloadFilePojo> data) {
        if (data == null) {
            data = new ArrayList<>();
        }

        this.data = data;
    }

    public void add(DownloadFilePojo toBeDownloaded) {
        if (!data.contains(toBeDownloaded)) {
            data.add(toBeDownloaded);
            fireTableDataChanged();
        }
    }

    public void delete(DownloadFilePojo toBeDownloaded) {
        data.remove(toBeDownloaded);
        fireTableDataChanged();
    }

    public DownloadFilePojo getDownloadFile(int row) {
        return data.get(row);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int column) {
        return COLUMNS[column];
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        DownloadFilePojo pojo = data.get(rowIndex);
        switch (columnIndex) {
            case FILE:
                return pojo.getFile();
            case STATUS:
                return pojo.getStatus();
            default:
                return "[NOT IMPLEMENTED]";
        }
    }
}
